package com.steak.feiyan.utils;

import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.net.URISyntaxException;

/**
 * @Author 刘牌
 * @Date 2020/2/17 0017 19:06
 * @Version 1.0
 */

public class HttpClient {
    /**
     * get请求
     * @param URI
     * @param key
     * @param value
     * @return
     * @throws URISyntaxException
     * @throws IOException
     */
    public static JSONObject httpGet(String URI , String key , String value) throws URISyntaxException, IOException {
        CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
        URIBuilder builder = new URIBuilder(URI);
        builder.addParameter(key,value);
        HttpGet httpGet = new HttpGet(builder.build());
        CloseableHttpResponse response = closeableHttpClient.execute(httpGet);
        int code = response.getStatusLine().getStatusCode();
        HttpEntity entity = response.getEntity();
        String content = EntityUtils.toString(entity,"utf-8");
        JSONObject jsonObject = JSONObject.parseObject(content);
        return jsonObject;
    }
}
